using System;
using Microsoft.Practices.Unity;

namespace PicStore.Common.Ioc
{
    public class ObjectFactory {

        public static void Configure(IUnityContainerBuilder builder)
        {
            Container = builder.BuildUnityContainer();
        }    

        public static IUnityContainer Container {
            get;
            set;
        }

        public static T GetInstance<T>() {
            return Container.Resolve<T>();
        }

        public static T GetInstance<T>(params object[] objects)
        {
            var instance = GetInstance<T>();

            Type type = instance.GetType();

            Container.Configure<InjectedMembers>()
                .ConfigureInjectionFor(type,new InjectionConstructor(objects));

            return GetInstance<T>();
        }

        public static T BuildUp<T>(T existing)
        {
            return Container.BuildUp<T>(existing);
        }

        public static object GetInstance(Type type)
        {
            return Container.Resolve(type);
        }

    }
}